package com.ek.controller;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.h2.expression.Variable;

import com.ek.model.Ueditor;
import com.ek.util.ConstantVars;
import com.ek.util.GetRandomID;
import com.jfinal.aop.Before;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.GET;
import com.jfinal.ext.interceptor.POST;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.render.Render;

public class NewsController extends Controller{
	public void index(){
		render("/jsp/news/list.jsp");
	}
	@Before(GET.class)
	public void getNewsList(){
		Map<String, Object > map = new HashMap();
		Integer pageSize = getParaToInt("rows");
		Integer pageNum = getParaToInt("page");
		if(pageNum == null)pageNum =1;
		if(pageSize == null)pageSize =10;
		String startTime = getPara("startTime");
		String endTime = getPara("endTime");
		String searchTitle = getPara("searchTitle");
		String sqlString="";
		Page<Ueditor>pageList;
		if(startTime != null && !startTime.isEmpty()){
			sqlString = sqlString+"from ueditor where time >= '"+startTime+"'";
		}
		if(endTime != null && !endTime.isEmpty()){
			if(sqlString.isEmpty()) sqlString = sqlString+"from ueditor where time <= '"+endTime+"'";
			else sqlString = sqlString+"and time <=  '"+endTime+"'";
			}
		if(searchTitle != null && !searchTitle.isEmpty()){
			if(sqlString.isEmpty()) sqlString = sqlString+"from ueditor where title like '%"+searchTitle+"%'";
			else sqlString = sqlString+"and title like '%"+searchTitle+"%'";
		
		}
		if(sqlString.isEmpty()){ 
			pageList =  Ueditor.dao.paginate(pageNum, pageSize);
			}
		else {
			pageList =  Ueditor.dao.paginate1(pageNum.intValue(), pageSize.intValue(),sqlString);
			}
		map.put("total", pageList.getTotalRow());
		map.put("rows", pageList.getList());
		renderJson(map);
	}
	@Before(POST.class)
	public void create(){
		String title = getPara("title");
		String source = getPara("source");
		String creater = getPara("creater");
		String content = getPara("content");
		String imgPath = getPara("imgPath");
		Ueditor ueditor  = getModel(Ueditor.class);
		GetRandomID r = new GetRandomID();
		String ID = r.randomId();
		ueditor.setID(ID);
		ueditor.setTitle(title);
		ueditor.setCreater(creater);
		ueditor.setSource(source);
		ueditor.setContent(content);
		ueditor.setTime(new Date());
		ueditor.setImgPath(imgPath);
		ueditor.save();
		redirect("/news");
	}
	public void delete(){
		String ID = getPara("ID");
		Ueditor ueditor = Ueditor.dao.findById(ID);
		String imgPath = ConstantVars.REAL_PATH+"image/"+ueditor.getImgPath();
		File file = new File(imgPath);
		if(file.isDirectory()){
			File[] files=file.listFiles();
		    for(int i=0;i<files.length;i++){files[i].delete();}		 
		}
		file.delete();
		ueditor.delete();
		redirect("/news");
	}
	public void edit(){
		String ID= getPara("ID");
		String title = getPara("title");
		String source = getPara("source");
		String creater = getPara("creater");
		String content = getPara("content");
		Ueditor ueditor  = Ueditor.dao.findById(ID);
		ueditor.setTitle(title);
		ueditor.setSource(source);
		ueditor.setContent(content);
		ueditor.setCreater(creater);
		ueditor.update();
		redirect("/news/getNewsByID?ID="+ID);
	}
public void getNewsByID(){
	String ID = getPara("ID");
	Ueditor ueditor = Ueditor.dao.findById(ID);
	setAttr("news", ueditor);
	render("/jsp/news/edit.jsp");
} 
}
